package me.firstapp.config;

import java.io.IOException;
import java.util.Properties;

import org.springframework.beans.factory.config.PropertiesFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import me.firstapp.spi.bbs.TopicService;
import me.firstapp.spi.shop.ProductService;

@Configuration
public class RootConfig {

	@Bean
	public PropertiesFactoryBean configProperties() {
		PropertiesFactoryBean configProperties = new PropertiesFactoryBean();
		configProperties.setLocations(new ClassPathResource("config.properties"));
		return configProperties;
	}

	// ==============================缓存配置start========================================
	@Bean
	public RedisConnectionFactory redisCF(PropertiesFactoryBean configProperties) {
		JedisConnectionFactory cf = new JedisConnectionFactory();
		try {
			Properties properties = configProperties.getObject();
			cf.setHostName(properties.getProperty("redis.host"));
			cf.setPort(Integer.valueOf(properties.getProperty("redis.port", "6379")));
			cf.setPassword(properties.getProperty("redis.password"));

		} catch (IOException e) {
			e.printStackTrace();
		}

		return cf;
	}

	@Bean
	public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisCF) {
		RedisTemplate<String, Object> redis = new RedisTemplate<>();
		redis.setConnectionFactory(redisCF);
		return redis;
	}

	@Bean
	public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory redisCF) {
		return new StringRedisTemplate(redisCF);
	}

	// ==============================缓存配置end========================================

	// ==============================远程服务配置start========================================
	@Bean
	public HttpInvokerProxyFactoryBean topicService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/topic.service");
			proxy.setServiceInterface(TopicService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}

	@Bean
	public HttpInvokerProxyFactoryBean productService(PropertiesFactoryBean configProperties) {
		HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
		try {
			Properties properties = configProperties.getObject();
			proxy.setServiceUrl(properties.getProperty("spi.server.url") + "/product.service");
			proxy.setServiceInterface(ProductService.class);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return proxy;
	}
	// ==============================远程服务配置end========================================
}
